<?php

namespace app\controller;


use app\service\UserTravelService;
use support\Response;
use think\Exception;
use think\facade\Db;

//气球旅行控制器
class UserTravelController extends Base
{


    public function index(): Response
    {
        $res = (new UserTravelService())->get_user_travel($this->user_id);
        return $this->json($res);
    }

    //出发
    public function start(): Response
    {
        $role_ids = $this->request->post('role_id_1', '0');
        // 启动事务
        Db::startTrans();
        try {
            $res = (new UserTravelService())->start($this->user_id, $role_ids);
            if ($res['code']) Db::commit();
            return $this->json($res);
        } catch (Exception $e) {
            Db::rollback();
            return $this->fail("数据库异常");
        }


    }

    //完成
    public function complete(): Response
    {
        $user_travel_id = $this->request->post('user_travel_id', '0');
        // 启动事务
        Db::startTrans();
        try {
            $res = (new UserTravelService())->complete($this->user_id, $user_travel_id);
            if ($res['code']) Db::commit();
            return $this->json($res);
        } catch (Exception $e) {
            Db::rollback();
            return $this->fail("数据库异常");
        }
    }

}